package model;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

public class Voto{
  
  private String numCandidato;
  
  public static String NOME_ARQUIVO;

  public Voto() {

  }

  public Voto(String numCandidato) {
    this.numCandidato = numCandidato;
  }

  public String getVoto() {
    return numCandidato;
  }

  public void setVoto(String numCandidato) {
    this.numCandidato = numCandidato;
  }
  
  private static String montaNomeArquivo(String cargo) {
    return "C:/Urna/Votos/"+cargo+".txt";
  }
  
  public static void inserir(Voto voto, String cargo) {
    
    NOME_ARQUIVO = montaNomeArquivo(cargo);
    
    try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(new File(NOME_ARQUIVO), true)))) {

      writer.write(voto.getVoto()+";\r\n");
      
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  public static void apurar(){
    
    deletarApuracaoOld();
    
    String linha;
    ArrayList<Voto> votos = new ArrayList<>();
    ArrayList<Cargo> allCargos = Cargo.getAllCargos();
    Map<String, Integer> count = new TreeMap<>();
    
    for(Cargo c : allCargos) {
      count.clear();
      votos.clear();
      
      try {
      
        BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Votos/"+c.getNome()+".txt"));

        while ((linha = reader.readLine()) != null) {

          Voto voto = new Voto();

          String[] t = linha.split(";");

          voto.setVoto(t[0]);
          
          votos.add(voto);
        }
        
        for(Voto voto: votos) {
          if(!count.containsKey(voto.getVoto())){
            count.put(voto.getVoto(), 0);
          }
          count.put(voto.getVoto(), count.get(voto.getVoto())+1);
        }
        
        for(Map.Entry<String, Integer> entry :  count.entrySet()) {
          PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(new File("C:/Urna/Apuracao/"+c.getNome()+".txt"), true)));
          writer.write( entry.getKey() + ";"
                       +entry.getValue()+ "\r\n");
          writer.close();
        }
        reader.close();
      } catch(IOException e) {
        e.printStackTrace();
      }
    }
  }
  
  public static int totalVotosEleicao() {
    
    String linha;
    int allVotos = 0;
    ArrayList<Cargo> allCargos = Cargo.getAllCargos();
    
    for(Cargo c : allCargos) {
      
      try {
      
        BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Apuracao/"+c.getNome()+".txt"));
        
        while ((linha = reader.readLine()) != null) {

          String[] t = linha.split(";");
          
          allVotos += Integer.parseInt(t[1]);
          
        }
        
        reader.close();
      } catch(IOException e) {
        e.printStackTrace();
      }
    }
    
    return allVotos;
  }
  
  public static Map<String, Integer> allVotosBrancos() {
    
    String linha;
    int counts;
    ArrayList<Cargo> allCargos = Cargo.getAllCargos();
    Map<String, Integer> allVotosBrancos = new TreeMap<>();
    
    for(Cargo c : allCargos) {
      counts = 0;
      try {
      
        BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Apuracao/"+c.getNome()+".txt"));

        while ((linha = reader.readLine()) != null) {

          String[] t = linha.split(";");
          if(t[0].equals("-2")) {
            counts += Integer.parseInt(t[1]);
          }
        }
        
        allVotosBrancos.containsKey(c.getNome());
        allVotosBrancos.put(c.getNome(), counts);
        reader.close();
      } catch(IOException e) {
        e.printStackTrace();
      }
    }
    
    return allVotosBrancos;
  }
  
  public static Map<String, Integer> allVotosNulos() {
    
    String linha;
    int counts;
    ArrayList<Cargo> allCargos = Cargo.getAllCargos();
    Map<String, Integer> allVotosNulos = new TreeMap<>();
    
    for(Cargo c : allCargos) {
      counts = 0;
      try {
      
        BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Apuracao/"+c.getNome()+".txt"));

        while ((linha = reader.readLine()) != null) {

          String[] t = linha.split(";");
          if(t[0].equals("-1")) {
            counts += Integer.parseInt(t[1]);
          }
        }
        
        allVotosNulos.containsKey(c.getNome());
        allVotosNulos.put(c.getNome(), counts);
        
      } catch(IOException e) {
        e.printStackTrace();
      }
    }
    
    return allVotosNulos;
  }
  
  public static int allVotosBrancoCargo(Cargo cargo){
    
    Map<String, Integer> count = allVotosBrancos();
    
    for(Map.Entry<String, Integer> entry :  count.entrySet()){
      if(cargo.getNome().equals(entry.getKey())) {
        return entry.getValue();
      }
    }
    return 0;
  }  
  
  public static int allVotosNuloCargo(Cargo cargo){
    
    Map<String, Integer> count = allVotosNulos();
    
    for(Map.Entry<String, Integer> entry :  count.entrySet()){
      if(cargo.getNome().equals(entry.getKey())) {
        return entry.getValue();
      }
    }
    return 0;
  }
  
  public static ArrayList<Candidato> executeApuracao(Cargo cargo) {
    
    ArrayList<Candidato>  listCand = Candidato.getAllCandidatos(cargo);
    String linha;   
    
    try {
      BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Apuracao/"+cargo.getNome()+".txt"));
      //
      for (Candidato cand: listCand) {       
        while ((linha = reader.readLine()) != null) {
          String[] t = linha.split(";");
          if(!(t[0].equals("-1") || t[0].equals("-2"))) {          
            if (cand.getNumeroCandidato() == Integer.parseInt(t[0])) {
              cand.setQtdeVotos(Integer.parseInt(t[1]));
              break;
            } else {
              cand.setQtdeVotos(0);
            }
          }         
        }
      }
      reader.close();
    } catch(IOException e) {
      e.printStackTrace();
    }
    // Ordenando candidatos pela quantidade de votos
    Collections.sort(listCand, new Candidato());
    return listCand;
  }
  
  public static int totalVotosCargo(Cargo cargo) {
    
    String linha;
    int allVotos = 0;
    
    try {

      BufferedReader reader = new BufferedReader(new FileReader("C:/Urna/Apuracao/"+cargo.getNome()+".txt"));

      while ((linha = reader.readLine()) != null) {

        String[] t = linha.split(";");

        allVotos += Integer.parseInt(t[1]);
          
      }
      reader.close();
    } catch(IOException e) {
      e.printStackTrace();
    }
    
    return allVotos;
  }
  
  private static void deletarApuracaoOld() {
    ArrayList<Cargo> allCargos = Cargo.getAllCargos();
    for(Cargo c : allCargos) {
      while(new File("C:/Urna/Apuracao/"+c.getNome()+".txt").exists()) {
        new File("C:/Urna/Apuracao/"+c.getNome()+".txt").delete();
      }
    }
  }
}